<?php
/**
 * 
 * Base YIC application class.
 * Sets up the environment as necessary and acts as the registry for the application.
 * Can broker to the autoload as well.
 *
 * @package YIC_Core
 * @author JayHoang
 *
 */
class YIC_Application extends Zend_Registry{
	
    /**
     *
     * Configuration instances
     *
     * @var array
     */
    private static $_configs;	
	
    /**
     * Helper method to remove the result of magic_quotes_gpc being applied to the
     * input super globals
     *
     * @param array The array to have slashes stripped, this is passed by reference
     * @param integer Recursion depth to prevent malicious use
     *
     * @return void
     */
    public static function undoMagicQuotes(&$array, $depth = 0) {
        if ($depth > 10 || !is_array($array)) {
            return;
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                self::undoMagicQuotes($array[$key], $depth + 1);
            } else {
                $array[$key] = stripslashes($value);
            }
            if (is_string($key)) {
                $new_key = stripslashes($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
    }

    /**
     *
     * Get configuration
     * Guarantee singelton
     *
     *
     * @param string $product
     * @param string $dbInstance
     *
     * @return Zend_Db object
     */
    public static function getConfig($namespace, $name, $configKey = '') {
        if (is_object(self::$_configs[$namespace][$name])) {
            if ($configKey) {
                return self::$_configs[$namespace][$name]->get($configKey);
            }
            return self::$_configs[$namespace][$name];
        }
        $configFilePath = APPLICATION_PATH . "/configs/$namespace/" . APPLICATION_ENV . ".$name.ini";
        if (!Zend_Loader::isReadable($configFilePath)) {
            throw new Exception("Configuration file not found: \"$configFilePath\"");
        }
        if (!$namespace) {
            throw new Exception("Configuration namespace \"$namespace\" not found in \"$configFilePath\"");
        }
        if (!$name) {
            throw new Exception("Configuration name \"$name\" not found in namespace \"$namespace\" in \"$configFilePath\"");
        }
//        $cache = GlobalCache::getCacheInstance('config');
//        $configKeyName = "config.$namespace.$name"; // config.namespace.name
//        $config = $cache->getCache($configKeyName);
	$config =false;
        if ($config === false) {
            $config = new Zend_Config_Ini($configFilePath, APPLICATION_ENV, true);
            //$cache->setCache($configKeyName, $config);
        }
        self::$_configs[$namespace][$name] = $config;
        if ($configKey) {
            return self::$_configs[$namespace][$name]->get($configKey);
        }
        return self::$_configs[$namespace][$name];
    }    
}